package tank;

import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.Random;

import abstractBeanFactory.BaseTank;

public class Tank extends BaseTank{
	public static final int TANK_WIDTH = ResourceMgr.goodTankD.getWidth();
	public static final int TANK_HEIGHT = ResourceMgr.goodTankD.getWidth();
	private static final int SPEED =5;
	private int x,y;
	Dir dir = Dir.DOWN;
	private boolean moving =true;
	TankFrame tf = null;
	private boolean living = true;
	
	public boolean isLiving() {
		return living;
	}
	private Random random =new Random();
	
	FireStrategy fireStrategy;
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
	public Tank(int x, int y, Dir dir,Group group,TankFrame tf) {
		super();
		this.x = x;
		this.y = y;
		this.dir = dir;
		this.group = group;
		this.tf = tf;
		rect.x= this.x;
		rect.y= this.y;
		rect.width= TANK_WIDTH;
		rect.height= TANK_HEIGHT;
		
		if(group==Group.GOOD) {
			String goodFSName = (String)PropertyMgr.get("goodFS");
			try {
				fireStrategy = (FireStrategy)Class.forName(goodFSName).getDeclaredConstructor().newInstance();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else { 
			String badFSName = (String)PropertyMgr.get("badFS");
			try {
				fireStrategy = (FireStrategy)Class.forName(badFSName).getDeclaredConstructor().newInstance();
//				fireStrategy = new DefalutFireStrategy();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	public void fire() {
		fireStrategy.fire(this);
	}
	public Dir getDir() {
		return dir;
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
	public boolean isMoving() {
		return moving;
	}
	private void move() {
		if(!moving) return;
		switch (dir) {
		case LEFT:
			x-=SPEED;
			break;
		case UP:
			y-=SPEED;
			break;
		case RIGHT:
			x+=SPEED;
			break;
		case DOWN:
			y+=SPEED;
			break;
		default:
			break;
		}
		if(this.group == Group.BAD && random.nextInt(100) > 95)this.fire();
		if(this.group == Group.BAD && random.nextInt(100) > 95)randomDir();
		//边界检测
		boundsCheck();
		this.rect.x = this.x;
		this.rect.y = this.y;
	}
	private void boundsCheck() {
		if(this.x<0)x=0;
		if(this.y<30)y=30;
		if(this.x>TankFrame.GAME_WIDTH-this.TANK_WIDTH)x=TankFrame.GAME_WIDTH-this.TANK_WIDTH;
		if(this.y>TankFrame.GAME_HEIGHT-this.TANK_HEIGHT)y=TankFrame.GAME_HEIGHT-this.TANK_HEIGHT;
	}
	private void randomDir() {
		this.dir = Dir.values()[random.nextInt(4)];
	}
	public void paint(Graphics g) {
		if(!living)tf.tanks.remove(this);
		
		switch (dir) {
			case LEFT:
				g.drawImage(this.group==Group.GOOD?ResourceMgr.goodTankL:ResourceMgr.badTankL, x, y, null);
				break;
			case UP:
				g.drawImage(this.group==Group.GOOD?ResourceMgr.goodTankU:ResourceMgr.badTankU, x, y, null);
				break;
			case RIGHT:
				g.drawImage(this.group==Group.GOOD?ResourceMgr.goodTankR:ResourceMgr.badTankR, x, y, null);
				break;
			case DOWN:
				g.drawImage(this.group==Group.GOOD?ResourceMgr.goodTankD:ResourceMgr.badTankD, x, y, null);
				break;
		default:
			break;
		}
	
		move();
		
	}
	public void setDir(Dir dir) {
		this.dir = dir;
	}
	public void setMoving(boolean moving) {
		this.moving = moving;
	}
	public void setX(int x) {
		this.x = x;
	}
	public void setY(int y) {
		this.y = y;
	}
	public void die() {
		this.living=false;
	}
	
}
